import 'package:components/lara_press_box.dart';
import 'package:components/lara_press_shimmer.dart';
import 'package:flutter/material.dart';
import 'package:common/export/tools.dart';
import 'package:common/utils/lara_img_help.dart';

// 根据图片比例展示图片
class LaraPressImgByRatio extends StatelessWidget {
  final String img;
  final double width;

  const LaraPressImgByRatio({
    super.key,
    required this.img,
    this.width = double.infinity,
  });

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<double?>(
      future: LaraPressImgHelp.getRatio(img),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return const Center(child: CircularProgressIndicator());
        }
        final ratio = snapshot.data!;
        return AspectRatio(
          aspectRatio: ratio,
          child: LaraPressImg(img: img, width: width, fit: BoxFit.contain),
        );
      },
    );
  }
}

class LaraPressImg extends StatelessWidget {
  final String img;
  final double width;
  final double height;
  final BoxFit fit;
  final double radius;
  const LaraPressImg({
    super.key,
    this.img = "",
    this.width = double.infinity,
    this.height = double.infinity,
    this.fit = BoxFit.cover,
    this.radius = 0,
  });
  @override
  Widget build(BuildContext context) {
    Widget widget = ExtendedImage.network(
      img,
      width: width,
      height: height,
      fit: fit,
      cache: true,
      loadStateChanged: (state) {
        if (state.extendedImageLoadState == LoadState.loading) {
          return LaraPressEmptyBox(
            child: LaraPressShimmer(
              width: width,
              height: height,
              radius: radius,
            ),
          );
        }
        if (state.extendedImageLoadState == LoadState.failed) {
          return LaraPressEmptyBox(
            child: Icon(
              Clarity.image_line,
              color: Theme.of(context).colorScheme.onSurfaceVariant,
              size: 40.sp,
            ),
          );
        }
        return null;
      },
    );
    if (radius > 0) {
      widget = ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: widget,
      );
    }
    return widget;
  }
}
